﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;
using Xiaov.SmartWeb.Infrastructure.Common;
using Xiaov.SmartWeb.Model.ActiveRecord;
using AjaxPro;
using Castle.ActiveRecord;
using Xiaov.SmartWeb.Model;

namespace Xiaov.SmartWeb.Application.SysAdmin
{
    public partial class Admin_Role : BasePage
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            //PM_FilterByProperty(ModulePropoty.MODULEURL, Request.Url.AbsolutePath);  
            PM_Filter();
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Admin_Role));
        }

        [AjaxPro.AjaxMethod]
        public string RolesString(object o)
        {
            return "";
        }

        [AjaxPro.AjaxMethod]
        public bool SaveRole(object roleid,object rolename,object comment,object disable,object sortby,object permissionstr)
        {
            try
            {
                Xiaov.SmartWeb.Model.ActiveRecord.S_PM_ROLE model = new S_PM_ROLE();
                if (!string.IsNullOrEmpty(roleid.ToString()))
                    model = Model.ActiveRecord.S_PM_ROLE.Find(roleid);
                if (model == null || string.IsNullOrEmpty(model.ROLEID))
                {
                    model.ROLEID = Guid.NewGuid().ToString();
                }
                else
                {
                    model.ROLEID = roleid.ToString();
                }
                model.ROLENAME = rolename.ToString();
                model.ROLECOMMENT = comment.ToString();
                if (disable == null)
                    disable = "false";
                model.DISABLED = (disable.ToString() == "false" ? "0" : "1");
                
                model.ROLESORT = sortby.ToInt32();
                //保存角色基本信息
                model.Save();
                model.RefreshCache();
                //保存角色权限信息
                //1、删除当前角色信息
                S_PM_ROLE_PERMISSION.DeleteAll("ROLEID='"+model.ROLEID+"'");
                //2、获取所有值
                List<Xiaov.SmartWeb.Model.Domain.ModulePermissionView> list = Model.Domain.ModulePermissionView.GetList(permissionstr);
                foreach (var q in list)
                {
                    S_PM_ROLE_PERMISSION pm = new S_PM_ROLE_PERMISSION();
                    pm.ROLE_MODULE = new ROLE_MODULE_KEY();
                    pm.ROLE_MODULE.ModuleID = q.ModuleId;
                    pm.ROLE_MODULE.ROLEID = model.ROLEID;
                    pm.PERMISSION = q.PermissionValue;
                    pm.Save();                        
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        

        [AjaxPro.AjaxMethod]
        public bool DeleteRole(object roleId)
        {
            try
            {
                Xiaov.SmartWeb.Model.ActiveRecord.S_PM_ROLE role = Model.ActiveRecord.S_PM_ROLE.Find(roleId);
                //删除用户角色对应表
                S_PM_USERROLES.DeleteAll("ROLEID='" + roleId + "'");
                S_PM_ROLE_PERMISSION.DeleteAll("ROLEID='" + roleId + "'");
                role.Delete();
                role.RefreshCache();
                return true;
            }
            catch
            {
                return false;
            }
            
        }
    }
}